package controller;


import model.Grammar;

import org.zkforge.ckez.CKeditor;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Textbox;


import admin.dao.GrammarDAO;

public class EditGrammarCtrl extends SelectorComposer<Component>{
	private static final long serialVersionUID = 1L;
	
	private Grammar currentGrammar;

	@Wire
    private Textbox txtName;
	
	@Wire
    private Textbox txtTitle;
	
	@Wire
    private CKeditor txtContent;
	
	
	@Listen("onClick = #btSubmit")
    public void submit(){
		currentGrammar.setTitle(txtTitle.getValue());
		currentGrammar.setName(txtName.getValue());
		currentGrammar.setContent(txtContent.getValue().toString());
		
		GrammarDAO.updateGrammar(currentGrammar);
		
		Sessions.getCurrent().removeAttribute("currentGrammar");
		Executions.getCurrent().sendRedirect("grammar_layout.jsp");
    }
	
	@Override
	public void doFinally() throws Exception {
		//get Grammar
		this.currentGrammar = (Grammar) Sessions.getCurrent().getAttribute("currentGrammar");
		
		
		txtContent.setValue(currentGrammar.getContent());
		txtName.setValue(currentGrammar.getName());
		txtTitle.setValue(currentGrammar.getTitle());
	}

}
